package org.aceor.tennis.web.handler;//package org.aceor.tennis.exception.handler;

import org.aceor.tennis.common.exception.BusinessRuntimeException;
import org.aceor.tennis.web.dto.BaseResult;
import org.aceor.tennis.web.util.WebUtil;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.servlet.http.HttpServletResponse;

@EnableWebMvc
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BusinessRuntimeException.class)
    public void handlerBizException(HttpServletResponse resp, BusinessRuntimeException exception) {
        log.warn("GlobalExceptionHandler$ e:{}", ExceptionUtils.getStackTrace(exception));
        BaseResult respObj = BaseResult.buildError(exception.getErrorCode(), exception.getErrorMessage());
        WebUtil.writeJsonResponse(resp, respObj);
    }

    @ExceptionHandler(Exception.class)
    public void handlerException(HttpServletResponse resp, Exception e) {
        log.warn(ExceptionUtils.getStackTrace(e));
        BaseResult respObj = BaseResult.buildError("系统繁忙,请稍后再试");
        WebUtil.writeJsonResponse(resp, respObj);
    }

}
